package com.flexcloudsdk.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flexcloudsdk.taskqueue.CloudQueueService;

public class CloudTaskQueueServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger
			.getLogger(CloudTaskQueueServlet.class.getName());
	@SuppressWarnings("unused")
	private CloudQueueService service = new CloudQueueService();
	private String sourceClass;
	private String sourceMethod;

	private enum CloudMethods {
		getDefaultQueueMethod, getQueueMethod, addMethod, deleteTaskMethod, getQueueNameMethod, purgeMethod, leaseTasksMethod, modifyTaskLeaseMethod, getHandleMethod, extractsParamsMethod, getEtaMillisMethod, getNameMethod, getPayloadMethod, getRetryCountMethod, createWithCountdownMillisMethod, createWithDefaultsMethod, createWithEtaMillisMethod, createWithHeaderMethod, createWithHeadersMethod, createWithMethod, createWithParamMethod, createWithPayLoadMethod, createWithRetyOptionsMethod, createWithTaskNameMethod, createWithUrlMethod, clearParamsMethod, getMethod, etaMillisMethod, getUrlMethod
	};

	public static final String getDefaultQueue = "getDefaultQueue";
	public static final String getQueue = "getQueue";

	public CloudTaskQueueServlet() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudOauthServlet.class.getName();
		log.info("*****CloudTaskQueueServlet constructor********");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		resp.setContentType("text/xml");
		PrintWriter writer = resp.getWriter();
		writer.println("<?xml version=\"1.0\"?>");
		writer.println("<root>");

		String operation = req.getParameter("operation");
		if (operation == null) {
			handleNullOperation(writer, req);
		} else if (operation.isEmpty()) {
			handleEmptyOperation(writer, req);
		} else {
			CloudMethods enumval = CloudMethods.valueOf(operation);
			switch (enumval) {
			case getDefaultQueueMethod:
				getDefaultQueue(writer, req, resp);
				break;
			case getQueueMethod:
				getQueue(writer, req, resp);
				break;
			case addMethod:
				add(writer, req, resp);
				break;
			case deleteTaskMethod:
				deleteTask(writer, req, resp);
				break;
			case getQueueNameMethod:
				getQueueName(writer, req, resp);
				break;
			case purgeMethod:
				purge(writer, req, resp);
				break;
			case leaseTasksMethod:
				leaseTasks(writer, req, resp);
				break;
			case modifyTaskLeaseMethod:
				modifyTaskLease(writer, req, resp);
				break;
			case getHandleMethod:
				getHandle(writer, req, resp);
				break;
			case extractsParamsMethod:
				extractsParams(writer, req, resp);
				break;
			case getEtaMillisMethod:
				getEtaMillis(writer, req, resp);
				break;
			case getNameMethod:
				getName(writer, req, resp);
				break;
			case getPayloadMethod:
				getPayload(writer, req, resp);
				break;
			case getRetryCountMethod:
				getRetryCount(writer, req, resp);
				break;
			case createWithCountdownMillisMethod:
				createWithCountdownMillis(writer, req, resp);
				break;
			case createWithDefaultsMethod:
				createWithDefaults(writer, req, resp);
				break;
			case createWithEtaMillisMethod:
				break;
			case createWithHeaderMethod:	
				break;
			case createWithHeadersMethod:
				break;
			case 	createWithMethod:
				break;
			case 	createWithParamMethod:
				break;
			case createWithPayLoadMethod: 	
				break;
			case 	createWithRetyOptionsMethod:
				break;
			case 	createWithTaskNameMethod:
				break;
			case createWithUrlMethod:
				break;
			case	clearParamsMethod:
				break;
			case 	getMethod:
				break;
			case 	etaMillisMethod:
				break;
			case 	getUrlMethod:
				break;
			default:
				break;
			}
		}	
		writer.println("</root>");
	}

	private void add(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void deleteTask(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getQueueName(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void purge(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void leaseTasks(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void modifyTaskLease(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getHandle(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void extractsParams(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getEtaMillis(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getName(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getPayload(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getRetryCount(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void createWithCountdownMillis(PrintWriter writer,
			HttpServletRequest req, HttpServletResponse resp) {
		
	}

	private void createWithDefaults(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void getQueue(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	}

	private void getDefaultQueue(PrintWriter writer, HttpServletRequest req, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
	}

	private void handleNullOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		log.warning("operation is null");
		writer.println("<result>No operation found while calling "
				+ req.getServerName() + "servlet</result>");
	}

	private void handleEmptyOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		log.warning("operation is empty");
		writer.println("<result>Must pass an operation in calling "
				+ req.getServerName() + "servlet</result>");
	}

	public static void main(String[] args) {
	}

}
